package com.xcdh.ittools.factory;

import com.api.ittools.entity.Tables;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ValueBeanHandler {
    private List<ValueBean> valueBeans ;
    private Class clazz ;
    private Object bean ;

    public ValueBeanHandler(Object bean){
        this.bean = bean ;
        this.clazz = bean.getClass() ;
        Field[] fields = clazz.getDeclaredFields();
        List<ValueBean> valueBeans = new ArrayList<>(fields.length) ;
        Method[] methods = clazz.getDeclaredMethods();
        Map<String, Method> map = Arrays.stream(methods)
                .collect(Collectors.toMap(Method::getName, Function.identity()));
        for (Field field : fields) {
            String fieldName = field.getName() ;
            String set = String.format("set%s",firstCharUpperCase(fieldName)) ;
            String get = String.format("get%s",firstCharUpperCase(fieldName)) ;
            if(map.containsKey(set) && map.containsKey(get) ){
                Method getMethod = map.get(get);
                Method setMethod = map.get(set);
                ValueBean valueBean = new ValueBean(fieldName , getMethod , setMethod) ;
                valueBeans.add(valueBean) ;
            }

        }
        this.valueBeans = valueBeans ;
    }
    private String firstCharUpperCase(String ch){
        char[] charArray = ch.toCharArray();
        charArray[0] = Character.toUpperCase(charArray[0]);
        return new String(charArray);
    }



    public Object get(String fieldName){
        ValueBean valueBean = getValueBean(fieldName);
        if(Objects.nonNull(valueBean)){
            try {
                return valueBean.handlerGet(bean);
            }catch (IllegalAccessException|InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
        throw new RuntimeException(String.format("方法%s不存在",fieldName)) ;
    }

    public void set(String fieldName , Object ...args){
        ValueBean valueBean = getValueBean(fieldName);
        if(Objects.nonNull(valueBean)){
            try {
                valueBean.handlerSet(bean , args);
                return;
            }catch (IllegalAccessException|InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
        throw new RuntimeException(String.format("方法%s不存在",fieldName)) ;
    }
    private ValueBean getValueBean(String fieldName){
        for (ValueBean valueBean : valueBeans) {
            if(Objects.equals(fieldName , valueBean.getFiledName())){
                return valueBean;
            }
        }
        return null ;
    }

    public static void main(String[] args) {
        Tables tables = new Tables();
        ValueBeanHandler handler = new ValueBeanHandler(tables) ;
        handler.set("aaa",1);
        System.out.println(handler.get("engine"));
    }

}
